package cn.iocoder.lw.module.core.controller.admin.device;

import cn.iocoder.lw.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.lw.framework.common.pojo.CommonResult;
import cn.iocoder.lw.framework.common.pojo.PageResult;
import cn.iocoder.lw.framework.common.util.object.BeanUtils;
import cn.iocoder.lw.framework.excel.core.util.ExcelUtils;
import cn.iocoder.lw.module.core.controller.admin.device.vo.*;
import cn.iocoder.lw.module.core.service.device.CoreDeviceService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

import static cn.iocoder.lw.framework.apilog.core.enums.OperateTypeEnum.*;
import static cn.iocoder.lw.framework.common.pojo.CommonResult.success;
import static cn.iocoder.lw.framework.common.pojo.PageParam.PAGE_SIZE_NONE;

@Tag(name = "管理后台 - 核心设备")
@RestController
@RequestMapping("/core/device")
@Validated
public class CoreDeviceController {

    @Resource
    private CoreDeviceService deviceService;

    @PostMapping("/create")
    @Operation(summary = "创建设备")
    @PreAuthorize("@ss.hasPermission('core:device:create')")
    @ApiAccessLog(operateType = CREATE)
    public CommonResult<Long> createDevice(@Valid @RequestBody CoreDeviceSaveReqVO createReqVO) {
        return success(deviceService.createDevice(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新设备")
    @PreAuthorize("@ss.hasPermission('core:device:update')")
    @ApiAccessLog(operateType = UPDATE)
    public CommonResult<Boolean> updateDevice(@Valid @RequestBody CoreDeviceSaveReqVO updateReqVO) {
        deviceService.updateDevice(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除设备")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('core:device:delete')")
    @ApiAccessLog(operateType = DELETE)
    public CommonResult<Boolean> deleteDevice(@RequestParam("id") Long id) {
        deviceService.deleteDevice(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得设备")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('core:device:query')")
    public CommonResult<CoreDeviceRespVO> getDevice(@RequestParam("id") Long id) {
        return success(deviceService.getDevice(id));
    }

    @GetMapping("/page")
    @Operation(summary = "获得设备分页")
    @PreAuthorize("@ss.hasPermission('core:device:query')")
    public CommonResult<PageResult<CoreDeviceRespVO>> getDevicePage(@Valid CoreDevicePageReqVO pageReqVO) {
        return success(deviceService.getDevicePage(pageReqVO));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出设备 Excel")
    @PreAuthorize("@ss.hasPermission('core:device:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportDeviceExcel(@Valid CoreDevicePageReqVO pageReqVO,
                                 HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PAGE_SIZE_NONE);
        PageResult<CoreDeviceImportExcelVO> pageResult = deviceService.getDeviceList(pageReqVO);
        // 导出 Excel
        ExcelUtils.write(response, "设备数据.xls", "设备列表", CoreDeviceImportExcelVO.class,
                BeanUtils.toBean(pageResult.getList(), CoreDeviceImportExcelVO.class));
    }

    @GetMapping("/project/{projectId}")
    @Operation(summary = "根据项目编号获取设备列表")
    @Parameter(name = "projectId", description = "项目编号", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('core:device:query')")
    public CommonResult<List<CoreDeviceRespVO>> getDeviceListByProjectId(@PathVariable("projectId") Long projectId) {
        return success(deviceService.getDeviceListByProjectId(projectId));
    }

    @GetMapping("/count-by-project")
    @Operation(summary = "根据项目编号和设备状态统计设备数量")
    @Parameter(name = "projectId", description = "项目编号", required = true, example = "1")
    @Parameter(name = "status", description = "设备状态", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('core:device:query')")
    public CommonResult<Long> countByProjectIdAndStatus(@RequestParam("projectId") Long projectId,
                                                        @RequestParam("status") Integer status) {
        return success(deviceService.countByProjectIdAndStatus(projectId, status));
    }

}